package org.example.jjj;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private StudentService studentService = new StudentService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String pageNumStr = request.getParameter("pageNum");
        int pageNum = 1;
        if (pageNumStr != null && !pageNumStr.isEmpty()) {
            pageNum = Integer.parseInt(pageNumStr);
        }
        List<Student> studentList = studentService.getStudentsByPage(pageNum);
        int totalCount = studentService.getTotalCount();
        int totalPages = (totalCount % StudentDAO.PAGE_SIZE == 0) ? totalCount /
                StudentDAO.PAGE_SIZE : totalCount / StudentDAO.PAGE_SIZE + 1;
        request.setAttribute("studentList", studentList);
        request.setAttribute("pageNum", pageNum);
        request.setAttribute("totalPages", totalPages);
        request.getRequestDispatcher("/student_list.jsp").forward(request, response);
    }
}